package kickboxing.view;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import kickboxing.controller.UzytkownikManager;
import kickboxing.model.Uzytkownik;

import org.apache.commons.beanutils.BeanUtils;

public class LogujAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public LogujAction() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html; charset=UTF-8");
		
		Uzytkownik user = new Uzytkownik();
		UzytkownikManager linkController = new UzytkownikManager();
		
		try {
			BeanUtils.populate(user, request.getParameterMap());
			
			if(linkController.loguj(user.getLogin(), user.getHaslo(), request) == true){
				
				
				
				user = linkController.zaladujUzytkownika(user.getLogin());
				
				String message = "Witaj " + user.getLogin();
				request.setAttribute("message", message);
				String admin = (String) request.getAttribute("admin");
				
				if(admin.equals("TAK")){
					HttpSession sesja = request.getSession(true);
					sesja.setAttribute("user", user);
					
					RequestDispatcher dispatcher = request.getRequestDispatcher("checkLogin.jsp");
					dispatcher.forward(request, response); 
				}else{
					String aktywacja = (String) request.getAttribute("aktywacja");
					if(aktywacja.equals("TAK")){
						
						response.setHeader("Cache-Control","no-store"); 
						response.setHeader("Pragma","no-cache"); 
						response.setDateHeader ("Expires", 0); 
						
						RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp");///userPanel.jsp
						dispatcher.forward(request, response); 
					}else{
						
						response.setHeader("Cache-Control","no-store"); 
						response.setHeader("Pragma","no-cache"); 
						response.setDateHeader ("Expires", 0); 
						
						message = " Twoje konto czeka na aktywacje";
						request.setAttribute("message", message);
						RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp");
						dispatcher.forward(request, response);
					}
					
				}
			}else{
				String message = "Niepoprawny login i haslo. Podaj poprawne dane";
				request.setAttribute("message", message);
				RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp");
				dispatcher.forward(request, response);
			}
			
			
			
			
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}

}
